package com.wfw.feichat.service.impl;

import com.wfw.feichat.entity.FriendRequest;
import com.wfw.feichat.entity.User;
import com.wfw.feichat.mapper.FriendRequestMapper;
import com.wfw.feichat.service.FriendRequestService;
import com.wfw.feichat.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: wfw
 * @date: 2019-05-21 16:31
 */
@Service
@Slf4j
public class FriendRequestServiceImpl implements FriendRequestService {
    @Autowired
    private FriendRequestMapper friendRequestMapper;

    @Autowired
    private UserService userService;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public FriendRequest getFriendRequestByUidAndFriendUid(String sendUid, String acceptUid) {
        if (StringUtils.isBlank(sendUid) || StringUtils.isBlank(acceptUid)) {
            return null;
        }

        FriendRequest friendRequest = new FriendRequest();
        friendRequest.setSendUid(sendUid);
        friendRequest.setAcceptUid(acceptUid);
        friendRequest = friendRequestMapper.selectOne(friendRequest);

        return friendRequest;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Integer addFriendRequest(String sendUid, String acceptUid) {
        if (StringUtils.isBlank(sendUid) || StringUtils.isBlank(acceptUid)) {
            return null;
        }
        FriendRequest friendRequest = getFriendRequestByUidAndFriendUid(sendUid, acceptUid);
        Integer result = 0;
        if (friendRequest != null) {
            friendRequest.setRequestTime(new Date());
            result = friendRequestMapper.updateByPrimaryKey(friendRequest);
        } else {
            User user1 = userService.getUserById(sendUid);
            User user2 = userService.getUserById(acceptUid);
            if (user1 == null || user2 == null) {
                return 0;
            }

            friendRequest = new FriendRequest();
            friendRequest.setRequestTime(new Date());

            String friendRequestId = Sid.nextShort();
            FriendRequest friendRequest1 = friendRequestMapper.selectByPrimaryKey(friendRequestId);
            while (friendRequest1 != null) {
                friendRequestId = Sid.nextShort();
                friendRequest1 = friendRequestMapper.selectByPrimaryKey(friendRequestId);
            }
            friendRequest.setId(friendRequestId);

            friendRequest.setSendUid(sendUid);
            friendRequest.setAcceptUid(acceptUid);

            result = friendRequestMapper.insert(friendRequest);
        }
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<FriendRequest> getFriendRequestListByAcceptUid(String acceptUid) {
        if (StringUtils.isBlank(acceptUid)) {
            return null;
        }
        FriendRequest friendRequest = new FriendRequest();
        friendRequest.setAcceptUid(acceptUid);

        return friendRequestMapper.select(friendRequest);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<String> getSendUidByAcceptUid(String acceptUid) {
        if (StringUtils.isBlank(acceptUid)) {
            return null;
        }
        return friendRequestMapper.getSendUidByAcceptUid(acceptUid);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Integer removeFriendRequest(String sendUid, String acceptUid) {
        FriendRequest friendRequest=new FriendRequest();
        friendRequest.setSendUid(sendUid);
        friendRequest.setAcceptUid(acceptUid);
        return friendRequestMapper.delete(friendRequest);
    }
}
